package com.example.myspringai.service.impl;

import com.example.myspringai.model.LlmChatParam;
import com.example.myspringai.service.ChatService;
import com.example.myspringai.tangguomeng.OpenAIByTang;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class ChatServiceImpl implements ChatService {
    @Resource(name = "ollamaChatModelLlama")
    private OllamaChatModel ollamaChatModelLlama;

    @Resource(name = "ollamaChatModelGemma")
    private OllamaChatModel ollamaChatModelGemma;

    @Resource(name = "ollamaChatModelMistral")
    private OllamaChatModel ollamaChatModelMistral;

    @Resource(name = "ollamaChatModelQwen")
    private OllamaChatModel ollamaChatModelQwen;

    @Resource(name = "ollamaChatModelLlamaUnsecure")
    private OllamaChatModel ollamaChatModelLlamaUnsecure;

    @Resource(name = "ollamaChatModelLlamaGuard")
    private OllamaChatModel ollamaChatModelLlamaGuard;

    @Resource(name = "ollamaChatModelJais")
    private OllamaChatModel ollamaChatModelJais;

    @Resource(name = "ollamaChatModelAya")
    private OllamaChatModel ollamaChatModelAya;

    @Resource(name = "ollamaChatModelSilma")
    private OllamaChatModel ollamaChatModelSilma;


    @Override
    public Flux<String> generateStream(LlmChatParam llmChatParam) {
        UserMessage userMessage = new UserMessage(llmChatParam.getMessage());
        if ("llama".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlama.stream(llmChatParam.getMessage());
        } else if ("mistral".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelMistral.stream(llmChatParam.getMessage());
        } else if ("gemma".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelGemma.stream(llmChatParam.getMessage());
        } else if ("qwen".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelQwen.stream(llmChatParam.getMessage());
        } else if ("llama-abliterated".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlamaUnsecure.stream(llmChatParam.getMessage());
        } else if ("guard".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlamaGuard.stream(llmChatParam.getMessage());
        } else if ("jais".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelJais.stream(llmChatParam.getMessage());
        } else if ("aya".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelAya.stream(llmChatParam.getMessage());
        } else if ("silma".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelSilma.stream(llmChatParam.getMessage());
        }
        return ollamaChatModelGemma.stream(userMessage);
    }

    @Override
    public String call(LlmChatParam llmChatParam) {
        if ("llama".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlama.call(llmChatParam.getMessage());
        } else if ("mistral".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelMistral.call(llmChatParam.getMessage());
        } else if ("gemma".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelGemma.call(llmChatParam.getMessage());
        } else if ("qwen".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelQwen.call(llmChatParam.getMessage());
        } else if ("llama-abliterated".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlamaUnsecure.call(llmChatParam.getMessage());
        } else if ("guard".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelLlamaGuard.call(llmChatParam.getMessage());
        } else if ("jais".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelJais.call(llmChatParam.getMessage());
        } else if ("aya".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelAya.call(llmChatParam.getMessage());
        } else if ("silma".equals(llmChatParam.getModel())) {
            return this.ollamaChatModelSilma.call(llmChatParam.getMessage());
        } else if ("gpt-4o".equals(llmChatParam.getModel())) {
            return OpenAIByTang.message(llmChatParam.getMessage());
        }
        return this.ollamaChatModelLlama.call(llmChatParam.getMessage());
    }
}
